<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class home extends core {
	
	public function __construct() {
		parent::__construct();
	}

	//首页接口
	public function index() {
		$flashes = $this->_flashes();
		$icons = $this->_icons();
		$yxjp = $this->_yxjp();
		//$hwjx = $this->_hwjx();
		// var_dump($icons);exit;
		success(array("flashes"=>$flashes, "icons"=>$icons, "yxjp"=>$yxjp));
	}

	//幻灯广告
	private function _flashes() {
		$now = time();
		$sql = "SELECT * FROM jf_ad WHERE position='flash' AND status='1' 
			ORDER BY sort ASC, id DESC LIMIT 5";	
		$ads = $this->db->get_results($sql);
		if ($ads) {
			$ads = add_domain_deep($ads, array("pic"));
		}
		return $ads;
	}	
	//分类图片
	private function _icons() {
		$now = time();
		$sql = "SELECT id , home_name , home_icon FROM jf_goods_category WHERE home_show= '1' AND status='1' ORDER BY sort ASC, id DESC LIMIT 2";	
		$icons = $this->db->get_results($sql);
		if ($icons) {
			$icons = add_domain_deep($icons, array("home_icon"));
		}
		return $icons;
	}
	//优选精品
	private function _yxjp() {
		$now = time();
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		$sql = "SELECT * FROM jf_ad WHERE position='yxjp' AND status='1' 
			ORDER BY sort ASC, id DESC LIMIT {$offset}, {$pagesize}";	
		$ads = $this->db->get_results($sql);
		if ($ads) {
			$ads = add_domain_deep($ads, array("pic"));
		}
		return $ads;
	}	

	//精选
	private function _hwjx() {
		$now = time();
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		$sql = "SELECT * FROM jf_ad WHERE position='hwjx' AND status='1' 
			ORDER BY sort ASC, id DESC LIMIT {$offset}, {$pagesize}";	
		$ads = $this->db->get_results($sql);
		if ($ads) {
			$ads = add_domain_deep($ads, array("pic"));
		}
		return $ads;
	}
	
	//搜索历史
	public function search_history() {
		$sh = null;
		if ($this->member_id) {
			$sql = "SELECT * FROM jf_seach_history WHERE member_id='{$this->member_id}' ORDER BY id DESC LIMIT 20";
			$sh = $this->db->get_results($sql);
		}
		success($sh);
	}
	
	//清空搜索历史
	public function clear_search_history() {
		$sql = "DELETE FROM jf_seach_history WHERE member_id='{$this->member_id}'";
		$this->db->query($sql);
		success('ok');
	}
	
	//删除搜索历史
	public function delete_search_history() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$sql = "DELETE FROM jf_seach_history WHERE member_id='{$this->member_id}' AND id='{$id}'";
		$this->db->query($sql);
		success('ok');
	}

	//热门关键词
	public function hotkeyword() {
		$keyword_type = (isset($_GET['type']) && trim($_GET['type'])) ? trim($_GET['type']) : 'tousu';
		$sql = "SELECT name FROM jf_hotkeyword WHERE keyword_type='{$keyword_type}' ORDER BY sort ASC, id DESC LIMIT 6";
		$keywords = $this->db->get_results($sql);
		success($keywords);
	}
}
